package com.data.config;

import com.data.entity.Tenant;
import com.data.service.TenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author ytj
 * @date 2024/4/19
 */
@Slf4j
@Configuration
public class DynamicDataSourceInit {
    @Resource
    private TenantService tenantService;
    @Resource
    private DynamicDataSource dataSource;

    /**
     * 添加数据源
     * */
    @PostConstruct
    public void initDataSource(){
        //加载租户数据源到动态数据源中
        List<Tenant> tenantList = this.tenantService.selectAll();
        tenantList.forEach(data->dataSource.setDataSoure(data));
    }

}
